//
//  SettingViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/18.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"

#import "SuggestionViewController.h"
#import "ClearCaches.h"
#import "JTNavigationController.h"
#import "AboutUsController.h"

static NSString *settingCellID = @"settingCellID";

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *settingTableView;

@property (strong, nonatomic) UIImageView *iconIV;
@property (strong, nonatomic) UILabel *realNameL;

@property (strong, nonatomic) NSArray *array;

@property (copy, nonatomic) NSString *cachesSize;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"设置";
    
    [self.view addSubview:self.settingTableView];
    __weak typeof(self) weakSelf = self;
    [self.settingTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        
    }];
    
    [self getCachesSize];
    
    [self createHeader];
    
    
    UIButton *logOutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [logOutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    logOutButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [logOutButton setTitleColor:Color333333 forState:UIControlStateNormal];
    [self.view addSubview:logOutButton];
    [logOutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.mas_equalTo(-60);
        make.size.mas_equalTo(CGSizeMake(200, 40));
    }];
    MKViewBorderRadius(logOutButton, 10, 0, WhiteColor);
    [logOutButton addTarget:self action:@selector(logOutClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)logOutClick {
    
    NSLog(@"退出登录");
    [CommonUserInfo sharedInstance].phone = nil;
    [CommonUserInfo sharedInstance].userInfoModel = nil;
    
    [CommonUserInfo sharedInstance].isLogin = NO;
    [CommonUserInfo sharedInstance].is_setPayPassword = NO;
    [CommonUserInfo sharedInstance].VIPLevel = @"";
    [CommonUserInfo sharedInstance].is_Bind = NO;
    [CommonUserInfo sharedInstance].nickName = @"";
    [CommonUserInfo sharedInstance].photoUrl = @"";
    [CommonUserInfo sharedInstance].iconIV = nil;
    [CommonUserInfo sharedInstance].balance = @"";
    [CommonUserInfo sharedInstance].beanNum = @"0.0";
    [CommonUserInfo sharedInstance].token = nil;
    [CommonUserInfo sharedInstance].user_id = @"";
    
    [[ALBBSDK sharedInstance] logout];
    
    [NSUserDefaults resetStandardUserDefaults];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
    [[SDImageCache sharedImageCache] clearMemory];
    
    [self.navigationController popViewControllerAnimated:NO];
    
}

#pragma mark -获取缓存大小
- (void)getCachesSize {

    NSString *docPath = [self getPath];
    self.cachesSize = [ClearCaches getCacheSizeWithFilePath:docPath];
    
}

- (NSString *)getPath {
   
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    return docPath;
    
}

- (void)createHeader {
    
    UIView *headerV = [[UIView alloc] init];
    headerV.backgroundColor = WhiteColor;
    headerV.frame = CGRectMake(0, 0, WIDTH, jisuan(240));
    
    UIImageView *iconIV = [[UIImageView alloc] init];
    self.iconIV = iconIV;
    iconIV.contentMode = UIViewContentModeScaleAspectFit;
    iconIV.layer.cornerRadius = jisuan(60);
    iconIV.layer.masksToBounds = YES;
    
//    self.iconIV.image = [CommonUserInfo sharedInstance].iconIV;
    
    if ([CommonUserInfo sharedInstance].photoUrl.length > 0) {

//        [iconIV sd_setImageWithURL:[NSURL URLWithString:[CommonUserInfo sharedInstance].photoUrl]];

        [iconIV sd_setImageWithURL:[NSURL URLWithString:[CommonUserInfo sharedInstance].photoUrl]];
//        iconIV.image = [CommonUserInfo sharedInstance].iconIV;

    }else {

        iconIV.image = [UIImage imageNamed:@"iconHeader"];

    }
    
    [headerV addSubview:iconIV];
    [iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.centerX.mas_equalTo(headerV.mas_centerX);
        make.top.mas_equalTo(headerV.mas_top).with.offset(Margin);
        make.width.mas_equalTo(jisuan(120));
        make.height.mas_equalTo(jisuan(120));
        
    }];
    
    UILabel *realNameL = [[UILabel alloc] init];
    self.realNameL = realNameL;
    
    if ([CommonUserInfo sharedInstance].nickName.length > 0) {
        
        realNameL.text = [CommonUserInfo sharedInstance].nickName;
        
    }else {
        
        realNameL.text = @"未登录";
        
    }
    
    realNameL.textColor = Color333333;
    realNameL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(28)];
    realNameL.textAlignment = NSTextAlignmentCenter;
    [headerV addSubview:realNameL];
    [realNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(headerV.mas_centerX);
        make.bottom.mas_equalTo(headerV.mas_bottom).with.offset(-jisuan(40));
   
    }];
    
    self.settingTableView.tableHeaderView = headerV;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(74);
}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCellID];
    
    if (!cell) {
        
        cell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settingCellID];
        
    }
    
    cell.type = indexPath.row;
    cell.dict = self.array[indexPath.row];
    
    if (indexPath.row == 2) {
        
        cell.cachesSize = self.cachesSize;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        
       NSLog(@"关于我们");
        
        [self.navigationController pushViewController:[AboutUsController new] animated:YES];
        
    }else if (indexPath.row == 1) {
        
        NSLog(@"意见反馈");
        SuggestionViewController *vc = [[SuggestionViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 2) {
        
        NSLog(@"清除缓存");
        NSString *docPath = [self getPath];
        [ClearCaches clearCacheWithFilePath:docPath];

        SettingTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.cachesSize = @"0.00";
        
    }else if (indexPath.row == 3) {
        
        NSLog(@"联系客服");
        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"010-53882711"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        /*
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]])
        {
            //用来接收临时消息的客服QQ号码(注意此QQ号需开通QQ推广功能,否则陌生人向他发送消息会失败)
            NSString *QQ = @"2604771282";
            //调用QQ客户端,发起QQ临时会话
            NSString *url = [NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",QQ];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }
    */
    }else {}
    
}

#pragma mark -lazy
- (UITableView *)settingTableView {
    
    if (!_settingTableView) {
        
        _settingTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _settingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _settingTableView.showsVerticalScrollIndicator = NO;
        _settingTableView.showsHorizontalScrollIndicator = NO;
        
        _settingTableView.estimatedRowHeight = 0;
        _settingTableView.estimatedSectionFooterHeight = 0;
        _settingTableView.estimatedSectionHeaderHeight = 0;
        
        _settingTableView.delegate = self;
        _settingTableView.dataSource = self;
        
        if (@available(iOS 11.0 , *)) {
            
            _settingTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    
    return _settingTableView;
    
}

- (NSArray *)array {
    
    if (!_array) {
        
        _array = @[
                   @{
                       @"title" : @"关于我们",
                       
                       },
                   @{
                       @"title" : @"意见反馈",
                       
                       },

                   @{
                       @"title" : @"清除缓存",
                       
                       },

                   @{
                       @"title" : @"联系客服",
                       
                       }
                   ];
        
    }
    
    return _array;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
